/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tests;

import Tests.StackPushThread;
import Sorting.Main;
import com.sun.j3d.utils.geometry.Box;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.vecmath.Vector3f;

/**
 *
 * @author rafael
 */
public class StackThread extends Thread {

    public static boolean isRunning = false;
    public static Vector3f elementPosition = new Vector3f(0.2f, -0.6f, -0.2f);

    public StackThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        execute();
    }

    private void execute() {
        
        try {
            Main.textPane.setText(Main.textoBubble);
            callStackButtons();
            Main.frame.setEnabled(false);

            BranchGroup bgroup = Main.stackBranchGroup();
            Main.su.addBranchGraph(bgroup);


        } finally {
            Main.isRunning = false;
            Main.clearTexts();
        }
    }

    private static void stackPushThreadCall() {

        if (!isRunning) {
            isRunning = true;
            StackPushThread thread = new StackPushThread("Push");
            thread.start();
        }
    }
    
    private static void checkElementPosition() {
        elementPosition.y = Main.stackSize * 0.4f;
    }

    public static void pushElement() {

        if (Main.stackSize == Main.stackCapacity) {
            JOptionPane.showMessageDialog(null, "Capacidade está no limite, não pode adicionar novos elementos.");
        } else {


        String numberString = "";
        int number = -1;

        while (number < 0 || number > 9) {
            numberString = JOptionPane.showInputDialog("Qual o número à ser adicionado?");
            
            if (numberString == null || numberString.equals("")) {    
            } else {
                number = Integer.parseInt(numberString);
            }
            
        }
        
        BranchGroup branchGroup = new BranchGroup();

        Box stackElement = new Box(0.4f, 0.15f, 0.4f, Main.criarApp());
        stackElement.setCapability(Box.ENABLE_APPEARANCE_MODIFY);
        
        Transform3D tfBox = new Transform3D();
        
        checkElementPosition();
        
        tfBox.setTranslation(new Vector3f(elementPosition.x, elementPosition.y, elementPosition.z));
        
        TransformGroup tgBox = new TransformGroup(tfBox);
        tgBox.addChild(stackElement);
        branchGroup.addChild(tgBox);

        Main.vetorNumeros[Main.stackSize] = new Sorting.Number(number, tgBox, 0);
        Main.stackSize++;
        
        Main.su.addBranchGraph(branchGroup);
        
        }
    }
    
    public static void popElement () {
        
        if (Main.stackSize == 0) {
            
            JOptionPane.showMessageDialog(null, "Pilha vazia, não há elementos à serem retirados!");
        
        } else {
        
        Main.destroySphereGeneric(Main.vetorNumeros, Main.stackSize - 1);
        Main.vetorNumeros[Main.stackSize - 1] = null;
        Main.stackSize--;
        
        }
        
    }
    
    public static void topElement () {
        
        if (Main.stackSize == 0) {
        
            JOptionPane.showMessageDialog(null, "Não há elementos na pilha.");
            
        } else {
        
            JOptionPane.showMessageDialog(null, "O elemento do topo da pilha é: " + Main.vetorNumeros[Main.stackSize - 1].numero);
        
        }
    }
    
    public static void showElements() {
        
        if (Main.stackSize == 0) {
        
            JOptionPane.showMessageDialog(null, "Não há elementos na pilha.");
            
        } else {
            
            
            String numeros = "{ ";
            
                for (int i = 0; i < Main.stackSize; i++) {
                    numeros += Main.vetorNumeros[i].numero + " ";
                }
            numeros += "}";
            
            JOptionPane.showMessageDialog(null, "Elementos: " + numeros);
        
        }
        
    }

    public static void callStackButtons() {


        JFrame frame = new JFrame("Stack Actions");

        JPanel stackButtonsPanel = new JPanel(new GridLayout(4, 0));

        JButton addButton = new JButton("Push");
        JButton removeButton = new JButton("Pop");
        JButton lastElementButton = new JButton("Top");
        JButton showElementsButton = new JButton("Stack Elements");

        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                pushElement();
            }
        });

        removeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                popElement();
            }
        });

        lastElementButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                topElement();
            }
        });

        showElementsButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
               showElements(); 
            }
            
        });
        
        JLabel exitLabel = new JLabel("Para finalizar a execução da pilha, feche esta janela");
        JTextArea exitText = new JTextArea("Para finalizar a execução da pilha, feche esta janela");
        exitText.setEditable(false);
        exitText.setLineWrap(true);
        exitText.setAutoscrolls(true);
        stackButtonsPanel.add(addButton);
        stackButtonsPanel.add(removeButton);
        stackButtonsPanel.add(lastElementButton);
        stackButtonsPanel.add(showElementsButton);
        frame.add(stackButtonsPanel);
        frame.setBounds(415, 60, 300, 140);

        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                    
                for (int i = 0; i < Main.stackSize; i++) {
                        Main.destroySphereStack(i);
                }
                Main.stackSize = 0;
                Main.isRunning = false;
                Main.clearTexts();
                Main.frame.setEnabled(true);
                
            }
        });

        frame.setResizable(false);
        frame.setVisible(true);

    }
}
